﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Guline.Web.CentralModels.Object
{
    public class CApp
    {
        [Key]
        public int ID { get; set; }
        [Display(Name = "lblAppName", ResourceType = typeof(Guline.Web.CentralLanguages.Main))]  
        [Required(ErrorMessageResourceName = "lblRequireName", ErrorMessageResourceType = typeof(Guline.Web.CentralLanguages.Validation))]
        public string Name { get; set; }

        public string Description { get; set; }

        public DateTime CreateDate { get; set; }

        public string Accesskey { get; set; }

        public string Secretkey { get; set; }


        public Nullable<int> DeviceID { get; set; }
        [ForeignKey("DeviceID")]
        public virtual Device Device { get; set; }

        public int AccountID { get; set; }
        [ForeignKey("AccountID")]

        public virtual Account Account { get; set; }

        public CApp()
        {
            CreateDate = DateTime.Now;
            GeneralKey();
        }

        public void GeneralKey()
        {
            this.Accesskey = randString();
            this.Secretkey = Guid.NewGuid().ToString();
        }
        private string randString()
        {
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            var stringChars = new char[8];
            var random = new Random();

            for (int i = 0; i < stringChars.Length; i++)
            {
                stringChars[i] = chars[random.Next(chars.Length)];
            }
            return new string(stringChars).ToUpper();
        }


        

    }
}
